import numpy as np
import matplotlib.pyplot as plt
#逻辑回归
from sklearn.linear_model import LogisticRegression
#留出法  37开
from sklearn.model_selection import train_test_split
#乳腺癌数据集
from sklearn.datasets import load_breast_cancer
#警告忽视
import warnings
warnings.filterwarnings('ignore')
#网格搜索交叉验证
from sklearn.model_selection import GridSearchCV
#加载数据
data=load_breast_cancer()
x=data.data
y=data.target
#切分训练集、测试集
train_x,test_x,train_y,test_y=train_test_split(x,y,train_size=0.7)
#创建逻辑回归模型
lr=LogisticRegression()
#设置网格搜索参数
pg={'C':[0.1,0.2,1,2,5,10],'penalty':['l1','l2']}
#创建网格搜索模型
gri=GridSearchCV(lr,pg,cv=10)
#传入数据
gri.fit(train_x,train_y)
#输出最优得分、最优参数
print('最优得分',gri.best_score_)
print('最优参数',gri.best_params_)

#重新训练模型
lr_best=LogisticRegression(penalty=gri.best_params_['penalty'],C=gri.best_params_['C'])
lr_best.fit(train_x,train_y)
#预测值
test_h=lr_best.predict(test_x)
#第一列0类别概率值
#负类别
#第二列1类别概率值
#正类别
y_predict_pro=lr_best.predict_proba(test_x)
print(y_predict_pro)
#获取正类别概率
y_predict_1=y_predict_pro[:,1]
# print(y_predict_1)
from sklearn.metrics import roc_curve,roc_auc_score

fpr,tpr,th=roc_curve(test_y,y_predict_1)
plt.plot(fpr,tpr)
plt.show()

print(roc_auc_score(test_y,y_predict_1))

import numpy as np
from sklearn.model_selection import learning_curve
li=list(np.linspace(0.01,1,50))

train_set_size,train_score,test_score=learning_curve(lr,
        train_x,train_y,cv=5,train_sizes=li)
#
print(train_score)
print(test_score)

train_score_a=np.mean(train_score,axis=1)
test_score_a=np.mean(test_score,axis=1)

train_score_s=np.std(train_score,axis=1)
test_score_s=np.std(test_score,axis=1)

plt.plot(train_set_size,train_score_a,'o-',c='r')
plt.plot(train_set_size,test_score_a,'o-',c='g')

plt.fill_between(train_set_size,train_score_a-train_score_s,train_score_a+train_score_s,alpha=0.1)
plt.fill_between(train_set_size,test_score_a-test_score_s,test_score_a+test_score_s,alpha=0.1)
plt.show()